/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.client.mvc.view;

import java.util.List;

import org.lifebudget.app.client.events.AppEvents;
import org.lifebudget.app.client.misc.NumberFormatter;
import org.lifebudget.app.client.mvc.model.CategoryTotal;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.google.gwt.core.client.GWT;

/**
 * The Class OverviewView displays the budget overview.
 * The budget overview contains all of the current, active Categories
 * and their totals.  This screen is used to view progress on the budget
 * at a glance.
 */
public class OverviewView extends View {
	
	/** The container. */
	private HtmlContainer container;
	
	/** The center. */
	private LayoutContainer center;
	
	/** The overview tab. */
	private TabItem overviewTab;

	/**
	 * Instantiates a new overview view.
	 *
	 * @param controller the controller
	 */
	public OverviewView(Controller controller) {
		super(controller);

	}

	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.mvc.View#initialize()
	 */
	public void initialize() {

	}

	/**
	 * Handles the <code>Init</code> event.  Calls the utility method
	 * to create the HTML view of the budget categories and their totals. 
	 * Then it adds it to the correct tab panel.
	 *
	 * @param event the <code>Init</code> event
	 */
	public void initUI(AppEvent event) {
		center = (LayoutContainer) Registry.get(AppView.CENTER_PANEL);
		overviewTab = (TabItem) Registry.get(TabView.OVERVIEW_TAB);
		container = new HtmlContainer();

		// createCategoryTotals(event);
		createHtmlCategoryTotals(event);

		// cp.add(vRowContainer);

		overviewTab.add(container);

		center.layout();
	}

	/**
	 * Utility method to create the html category totals.
	 *
	 * @param event the event
	 */
	private void createHtmlCategoryTotals(AppEvent event) {
		List<CategoryTotal> categoryTotals = (List<CategoryTotal>) event.getData();
		StringBuilder html = new StringBuilder();
		// divide the categories.size by 4
		// foreach group of 4, create a new layoutContainer
		// html.append("<div>");
		for (int c = 0; c < categoryTotals.size(); c++) {
			CategoryTotal ct = categoryTotals.get(c);
			GWT.log("creating new CategoryTotal " + c);
			html.append("<div class=\"categoryTotal\">");
			html.append("<span class=\"categoryHeading\">" + ct.getCategory().getName() + "</span><br/>");
			String total = NumberFormatter.getCurrencyFormattedString(ct.getTotal());
			html.append("<span class='categoryAmount'>" + total + "</span>");
			html.append("</div>");
		}
		// html.append("</div>");
		container.setHtml(html.toString());
	}

	/* (non-Javadoc)
	 * @see com.extjs.gxt.ui.client.mvc.View#handleEvent(com.extjs.gxt.ui.client.mvc.AppEvent)
	 */
	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI(event);
		}
		if (event.getType() == AppEvents.RefreshCategory || event.getType() == AppEvents.RefreshTransaction) {
			GWT.log("Overview view got a refresh event: " + event.getType().toString());
			onRefresh(event);
		}

	}

	/**
	 * Handles the <code>RefreshCategory</code> and <code>RefreshTransaction</code> 
	 * events.
	 *
	 * @param event the event
	 */
	private void onRefresh(AppEvent event) {
		createHtmlCategoryTotals(event);
		
	}
}
